package component
{
	import assets.Assets;
	
	import model.ControlVO;
	
	import mx.containers.ControlBar;
	import mx.controls.Text;
	
	import signal.ControlSignal;
	
	import starling.core.Starling;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.textures.Texture;
	import starling.textures.TextureAtlas;

	public class Macario extends Sprite
	{
		/**
		 * Guide To Macario PNG Sequence
		 * 1 = Stand
		 * 2 - 23 = Walk
		 * 24 - 36 = Run
		 * 37 - 45 = Run fast
		 * 46 - 55 = Skid
		 * 56 - 65 = Skid Opposite **/
		
		private static const FRAME_STAND:String = "marioassetStand_";
		
		private static const FRAME_WALK:String = "marioassetsRun_";//"marioassetsWalk_";
		
		private static const FRAME_RUN:String = "marioassetsRunFast_"//"marioassetsRun_"
		
		private static const FRAME_RUN_FAST:String = "marioassetsRunFast_";
		
		private static const FRAME_SKID:String = "marioassetsSkid_";
		
		private static const FRAME_SKID_OPPOSITE:String = "marioassetsSkidOpposite_";
		
		public static const STATE_STAND_RIGHT:int = 0;
		
		public static const STATE_WALK_RIGHT:int = 1;
		
		public static const STATE_RUN_RIGHT:int = 2;
		
		public static const STATE_STAND_LEFT:int = 3;
		
		public static const STATE_WALK_LEFT:int = 4;
		
		public static const STATE_RUN_LEFT:int = 5;
		
		private var macarioStandRight:MovieClip;
		
		private var macarioWalkRight:MovieClip
		
		private var macarioRunRight:MovieClip
		
		private var macarioRunFastRight:MovieClip
		
		private var macarioSkidRight:MovieClip
		
		private var macarioSkidOppositeRight:MovieClip;
		
		private var macarioStandLeft:MovieClip;
		
		private var macarioWalkLeft:MovieClip;
		
		private var macarioRunLeft:MovieClip;
		
		private var animationState:int;
		
		private var movieVector:Vector.<MovieClip>;
		
		public static const controlSignal:ControlSignal = new ControlSignal();
		
		public var isFacingRight:Boolean = true;
		
		private var isBPressed:Boolean;
		
		public function Macario()
		{
			super();
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStageHandler);
		}
		
		private function onAddedToStageHandler(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStageHandler);
			controlSignal.add(onSingalControlHandler);
			createCharacter();
		}
		
		private var newAnimationState:int;
		private function onSingalControlHandler(vo:ControlVO):void
		{
			switch(vo.pressed)
			{
				case ControlVO.PRESS_LEFT:
					newAnimationState = STATE_WALK_LEFT;
					break;
				case ControlVO.PRESS_RIGHT:
					newAnimationState = STATE_WALK_RIGHT;
					break;
				case ControlVO.DOUBLE_PRESS_RIGHT:
					newAnimationState = STATE_RUN_RIGHT;
					break;
				case ControlVO.PRESS_B:
					isBPressed = true;
					break;
				case ControlVO.RELEASE_LEFT:
					newAnimationState = STATE_STAND_LEFT;
					break;
				case ControlVO.RELEASE_RIGHT:
					newAnimationState = STATE_STAND_RIGHT;
					break;
				case ControlVO.RELEASE_B:
					isBPressed = false;
					break;
				case ControlVO.DOUBLE_PRESS_LEFT:
					newAnimationState = STATE_RUN_LEFT;
					break;
				
			}
		}
		
		private function createCharacter():void
		{
			var atlas:TextureAtlas = Assets.getAtlas(); 
			
			var standFrames:Vector.<Texture> = atlas.getTextures(FRAME_STAND);
			macarioStandRight = new MovieClip(standFrames, 1);
			macarioStandRight.loop = false;
			macarioStandRight.pivotX = macarioStandRight.width/2;
			animationState = STATE_STAND_RIGHT;
			
			macarioStandLeft = new MovieClip(standFrames, 1);
			macarioStandLeft.loop = false;
			macarioStandLeft.pivotX = macarioStandLeft.width/2;
			macarioStandLeft.scaleX = -1;
			
			var walkFrames:Vector.<Texture> = atlas.getTextures(FRAME_WALK);
			macarioWalkRight = new MovieClip(walkFrames, 30);
			macarioWalkRight.loop = true;
			macarioWalkRight.pivotX = macarioWalkRight.width/2;
			
			macarioWalkLeft = new MovieClip(walkFrames, 30);
			macarioWalkLeft.loop = true;
			macarioWalkLeft.pivotX = macarioWalkLeft.width/2;
			macarioWalkLeft.scaleX = -1;
			
			var runFrames:Vector.<Texture> = atlas.getTextures(FRAME_RUN);
			macarioRunRight = new MovieClip(runFrames, 30);
			macarioRunRight.loop = true;
			macarioRunRight.pivotX = macarioRunRight.width/2;
			
			macarioRunLeft = new MovieClip(runFrames, 30);
			macarioRunLeft.loop = true;
			macarioRunLeft.pivotX = macarioRunLeft.width/2;
			macarioRunLeft.scaleX = -1;
			
			movieVector = new Vector.<MovieClip>();
			movieVector[STATE_STAND_RIGHT] = macarioStandRight;
			movieVector[STATE_WALK_RIGHT] = macarioWalkRight;
			movieVector[STATE_RUN_RIGHT] = macarioRunRight;
			movieVector[STATE_STAND_LEFT] = macarioStandLeft;
			movieVector[STATE_WALK_LEFT] = macarioWalkLeft;
			movieVector[STATE_RUN_LEFT] = macarioRunLeft;
			
			this.pivotX = this.width/2;
			this.pivotY = this.height;
			
			addChild(movieVector[animationState]);
			Starling.juggler.add(movieVector[animationState]);
			
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		
		private function onEnterFrame(event:Event):void
		{
			if(newAnimationState == STATE_WALK_RIGHT && isBPressed)
				newAnimationState = STATE_RUN_RIGHT;
			
			if(newAnimationState == STATE_WALK_LEFT && isBPressed)
				newAnimationState = STATE_RUN_LEFT;
			
			if(newAnimationState != animationState) 
			{
				if(movieVector[animationState])
				{
					removeChild(movieVector[animationState]);
					Starling.juggler.remove(movieVector[animationState]);
				}
				
				animationState = newAnimationState;
				
				addChild(movieVector[animationState]);
				Starling.juggler.add(movieVector[animationState]);
			}
				
			
			switch(animationState) {
				case STATE_WALK_RIGHT:
					if(stage.stageWidth - this.width/2 > this.x)
						this.x += 7;
					break;
				case STATE_WALK_LEFT:
					if(this.width/2 < this.x)
						this.x -= 7;
					break;
				case STATE_RUN_RIGHT:
					if(stage.stageWidth - this.width/2 > this.x)
						this.x += 15;
					break;
				case STATE_RUN_LEFT:
					if(this.width/2 < this.x)
						this.x -= 15;
					break;
			}
		}
	}
}